The Scripting app allows you to retrieve workout sessions from HealthKit using the global Health.queryWorkouts()
function. Workouts represent physical activity sessions such as running, walking, swimming, cycling, strength training, and more.
Each workout includes metadata such as duration, activity type, start/end times, and detailed statistics like heart rate, distance, and energy burned.
A HealthWorkout record contains:
startDate
/ endDate
: The duration of the workout sessionduration
: Total duration in secondsworkoutActivityType
: Enum representing the workout type (e.g., running, walking)metadata
: Optional custom metadataworkoutEvents
: Optional array of workout-related events (e.g., pauses, laps)allStatistics
: A dictionary of detailed quantity statistics (e.g., heart rate, distance, calories)Parameter | Description |
---|---|
startDate / endDate |
Optional time range to filter workouts |
limit |
Maximum number of workouts to return |
strictStartDate / strictEndDate |
Whether to match the exact start/end dates |
sortDescriptors |
Sort results by startDate , endDate , or duration |
requestPermissions |
An array of health quantity types for which to request permissions before querying. |
The allStatistics
dictionary provides detailed quantity data recorded during the workout. You can extract values using:
Common available statistics include:
"heartRate"
"activeEnergyBurned"
"distanceWalkingRunning"
"stepCount"
If recorded, workout.workoutEvents
contains an array of time-stamped workout events:
Event types include: pause, resume, lap, segment, motion pause/resume, etc.
HealthWorkout
workoutActivityType
is an enum, which you can map to labels or iconsallStatistics
is missing some keys, the data may not have been recorded by the device/appTo read workouts from HealthKit:
Health.queryWorkouts(options)
to get a list of HealthWorkout
recordsstartDate
, endDate
, and sorting options to filter and order resultsduration
, activityType
, and allStatistics
for insightsworkoutEvents
and metadataThis API is ideal for analyzing exercise history, generating workout summaries, or visualizing fitness trends.